package com.globalwave.common.service;

import com.globalwave.base.BaseServiceImpl;
import com.globalwave.common.ArrayOrderList;
import com.globalwave.common.ArrayPageList;
import com.globalwave.common.entity.Field;
import com.globalwave.common.entity.FieldCriterion;

public class FieldServiceImpl 
       extends BaseServiceImpl 
       implements FieldService {

    public Field create(Field field) throws Exception {

    	
    	final Field result = (Field) jdbcDao.insert(field, true);
    	
    	return result ;
    }

    public void update(Field field) throws Exception {
    	//field.addIgnoreProperties("created_on") ;
    	//field.addIgnoreProperties("created_by") ;
        jdbcDao.update(field) ;
    }

    public void delete(Field field) throws Exception {
        
        jdbcDao.delete(field) ;
        
    }

    public void deleteAll(Long[] ids) throws Exception {
    	FieldCriterion criterion = new FieldCriterion() ;
        criterion.setIds(ids) ;
        jdbcDao.delete(Field.class, criterion) ;
        
    }

    public ArrayPageList<Field> query(
    		FieldCriterion fieldCriterion, 
            int pageIndex, 
            int pageSize, 
            ArrayOrderList orderList) throws Exception {
        
        if (orderList == null) {
            orderList = new ArrayOrderList() ;
            orderList.addAsc("index_") ;
            orderList.addAsc("field_id") ;
        }
        
        return (ArrayPageList<Field>)jdbcDao.query(fieldCriterion, pageIndex, pageSize, orderList, Field.class);
    }

	@Override
	public ArrayPageList<Field> queryByViewId(Long viewId) throws Exception {
    	FieldCriterion fieldCriterion = new FieldCriterion() ;
    	fieldCriterion.setView_id(viewId) ;

        return (ArrayPageList<Field>)query(fieldCriterion, ArrayPageList.PAGEINDEX_NO_PAGE, -1, null) ;
	}


    public int deleteByViewId(Long viewId) throws Exception {

    	FieldCriterion fieldCriterion = new FieldCriterion() ;
    	fieldCriterion.setView_id(viewId) ;

        return jdbcDao.delete(Field.class, fieldCriterion) ;
    }

    public int deleteByViewIds(Long[] viewIds) throws Exception {

    	FieldCriterion fieldCriterion = new FieldCriterion() ;
    	fieldCriterion.setView_ids(viewIds) ;

        return jdbcDao.delete(Field.class, fieldCriterion) ;
    }
}
